<!--个人中心的地址管理里显示的拥有的地址  -->
<template>
	<view class="page">
		<!-- 地址列表 -->
		<view class="address-list">
			<view class="list" v-for="(item,index) in addressTable" :key="index">
				<view class="name-phone">
					<view class="name">
						<text class="one-omit">{{item.userName}}</text>
					</view>
					<view class="phone">
						<text>{{item.userPhone}}</text>
						<span v-if="item.state == 34">
						<text class="tag">默认</text>
						</span>
					</view>
					<view class="edit" @click="addressDelete(item.addressId)">
						<text style="font-size: 14px;color: red;" class="iconfont">x</text>
					</view>
				</view>
				<view class="address-edit">
					<view class="address">
						<text>{{item.address}}</text>
					</view>
					<view class="edit" @click.stop="onAddressEdit(1,index)">
						<text class="iconfont icon-edit1"></text>
					</view>
				</view>
			</view>
		</view>
		<!-- 添加地址 -->
		<view class="add-address">
			<view class="btn" @click="onAddressEdit(2)">
				<text>新建收货地址</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {baseUrl} from '@/config/baseUrl.js'
	export default {
		data() {
			return {
				addressTable:[],
			};
		},
		onLoad() {
			this.lookUserAddress()
		},
		methods:{
			/**
			 * 编辑地址点击 
			 */
			onAddressEdit(type,index){
				if(type==1){
					uni.setStorageSync("AddressTable", this.addressTable[index]);
				}else{
					uni.setStorageSync("AddressTable", null);
				}
				uni.navigateTo({
					url: '/pages/AddressEdit/AddressEdit?type=' + type,
				})
			},
			/* 删除地址 */
			addressDelete(id){
				uni.request({
					url:  baseUrl+"/user/deleteAddress",
					method: 'POST',
					data: {
						addressId:id,
					},
					header:{ "Content-Type": "application/x-www-form-urlencoded"},
					success: (res) => {
						this.lookUserAddress()
					}
				})
			},
			/**
			 * 查找用户地址
			 */
			lookUserAddress(){
				let userId = uni.getStorageSync("userId")
				uni.request({
					url:  baseUrl+"/user/getUserAddress",
					method: 'POST',
					data: {
						userId:uni.getStorageSync("userId"),
					},
					header:{ "Content-Type": "application/x-www-form-urlencoded"},
					success: (res) => {
						this.addressTable = res.data.addressList
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'AddressList.scss';
</style>
